#coding=utf-8
from django.shortcuts import render,redirect
from django.http import HttpResponse,HttpResponseRedirect
# Create your views here.

def index(request):
    return HttpResponse('hello word')

def detail(request,id):
    return HttpResponse(id)


# 展示链接的页面
def getTest1(request):
    return render(request,'booktest/getTest1.html')
# 接受一键一值
def getTest2(request):
    a1 = request.GET['a']
    b1 = request.GET['b']
    c1 = request.GET['c']
    # context = {'a': a1, 'b': b1, 'c': c1}
    return render(request,'booktest/getTest2.html',locals())
# 接受一键多值
def getTest3(request):
    a1 = request.GET.getlist('a')[2].encode('utf-8')
    # context = {'a':a1}
    return render(request,'booktest/getTest3.html',locals())

def postTest1(request):
    return render(request,'booktest/postTest1.html')

def postTest2(request):
    uname = request.POST['uname']
    upwd = request.POST['upwd']
    ugender = request.POST['ugender']
    uhobby = request.POST.getlist('uhobby')
    # context = {'uname': uname,'upwd': upwd,'ugender': ugender,'uhobby': uhobby}
    return render(request,'booktest/postTest2.html',locals())

# cookie联系
def cookieTest(request):
    reponse = HttpResponse()
    cookie = request.COOKIES
    if cookie.has_key('t1'):
        reponse.write(cookie['t1'])
    # reponse.set_cookie('t1','abcd')
    return reponse

# 重定向
def redTest1(request):
    # return HttpResponseRedirect('/booktest/redTest2/')
    return redirect('/booktest/redTest2/')
def redTest2(request):
    return HttpResponse('转向后页面')


def session1(request):
    uname = request.session.get('myname','未登录')
    # context = {'uname':uname}
    return render(request,'booktest/session1.html', locals())

def session2(request):
    return render(request,'booktest/session2.html')

def session2_handle(request):
    uname = request.POST['uname']
    request.session['myname']=uname
    request.session.set_expiry(0)
    return redirect('/booktest/session1/')

def session3(request):
    request.session.flush()
    return redirect('/booktest/session1/')